import Vue from 'vue';
import Router from 'vue-router';
import NProgress from 'nprogress' // progress bar
import permission from './utils/permission.js';
import 'nprogress/nprogress.css' // progress bar style

import Login from './views/Login.vue'
import NotFound from './views/404.vue'
import Layout from './views/Layout.vue' //layout
import Main from './views/Main.vue' //layout

const routes = [{
        path: '/Login',
        component: Login,
        name: '登录',
        hidden: true
    },
    {
        path: '/',
        component: Layout,
        name: '主页',
        iconCls: 'el-icon-message', //图标样式class
        leaf: true, //只有一个节点
        // redirect: '/Main',
        children: [
            { path: '/Main', component: Main, name: '平台数据统计' },


        ]
    },

    {
        path: '/404',
        component: NotFound,
        name: 'Error',
        hidden: true
    },
    {
        path: '/class',
        component: Layout,
        name: '课程管理',
        children: [{
                name: "课程内容列表",
                path: '/class/list/Contentlist',
                component: () =>
                    import ('./views/class/courseTextList.vue'),
                hidden: true,
            },
            {
                name: "课程关联列表",
                path: '/class/list/Relationlist',
                component: () =>
                    import ('./views/class/courseRelationList.vue'),
                hidden: true,
            }
        ],
        hidden: true
    },
    {
        path: '/userstudyCourse',
        component: Layout,
        name: '用户学习管理',
        children: [{
            path: '/userstudyCourse/list',
            name: "用户学习课程",
            component: () =>
                import ('./views/charts/UserstudyCourseList.vue'),
            hidden: true,
        }],
        hidden: true
    },
    {
        path: '/CourseContentStudy',
        component: Layout,
        name: '课程内容管理',
        children: [{
            path: '/CourseContentStudy/list',
            name: "课程内容列表",
            component: () =>
                import ('./views/charts/CourseContentStudyList.vue'),
            hidden: true,
        }], 
        hidden: true
    },
    {
        path: '/CourseStudyDetail',
        component: Layout,
        name: '课程内容学习管理',
        children: [{
            path: '/CourseStudyDetail/list',
            name: "课程内容学习详情",
            component: () =>
                import ('./views/charts/CourseStudyDetail.vue'),
            hidden: true,
        }],
        hidden: true
    },
    {
        path: '/UserStudyCourseContentList',
        component: Layout,
        name: '用户学习课程内容管理',
        children: [{
            path: '/UserStudyCourseContentList/list',
            name: "用户学习课程内容列表",
            component: () =>
                import ('./views/charts/UserStudyCourseContentList.vue'),
            hidden: true,
        }],
        hidden: true
    },

    {
        path: '/train',
        component: Layout,
        name: '培训研修管理',
        children: [{
            name: "培训研修报名列表",
            path: '/train/trainSignList',
            component: () =>
                import ('./views//train/trainSignList.vue'),
            hidden: true,
        }],
        hidden: true
    },
      {
        path: '/teacher',
        component: Layout,
        name: '名师堂管理',
        children: [
          {
            name: "名师标签",
            path: '/teacher/teacherType',
            component: () =>
              import('./views/teacher/teacherType.vue'),
            hidden: true,
          }
        ],
        hidden: true
      },
    {
        path: '/xiangqing',
        component: Layout,
        name: '反馈详情',
        children: [{
                path: '/fankui/fankuixiangqing',
                component: () =>
                    import ('./views/fankui/fankuixiangqing.vue'),
                hidden: true,
            }

        ],
        hidden: true
    },
    {
        path: '/wenzhangxiangqing',
        component: Layout,
        name: '反馈详情',
        children: [{
                path: '/huanbaowenzhang/wenzhangxiangqing',
                component: () =>
                    import ('./views/huanbaowenzhang/wenzhangxiangqing.vue'),
                hidden: true,
            }

        ],
        hidden: true
    },
    {
        path: '/message',
        component: Layout,
        name: '发送消息详情',
        children: [{
                path: '/message/mesageDetails',
                component: () =>
                    import ('./views/message/messageDetails.vue'),
                hidden: true,
            }

        ],
        hidden: true
    },
    {
        path: '/dingdanxiangqing',
        component: Layout,
        name: '订单详情',
        children: [{
                path: '/jifenshangcheng/dingdanxiangqing',
                component: () =>
                    import ('./views/jifenshangcheng/dingdanxiangqing.vue'),
                hidden: true,
            }

        ],
        hidden: true
    },
    {
        path: '/toker',
        component: Layout,
        name: '拓客详情',
        children: [{
                path: '/toker/tokerDetails',
                component: () =>
                    import ('./views/Toker/tokerDetail.vue'),
                hidden: true,
            }

        ],
        hidden: true
    },
    {
        path: '/jifenshangcheng',
        component: Layout,
        name: '积分商品',
        children: [{
                path: '/jifenshangcheng/addGoods',
                component: () =>
                    import ('./views/jifenshangcheng/addGoods.vue'),
                hidden: true,
            }

        ],
        hidden: true
    },
    {
        path: '/user',
        component: Layout,
        name: '用户详情',
        children: [{
            path: '/user/userDetails',
            component: () =>
                import ('./views/user/userDetails.vue'),
            hidden: true,
        }],
        hidden: true
    },
    {
        path: '/black',
        component: Layout,
        name: '用户详情',
        children: [{
            path: '/black/blackDetails',
            component: () =>
                import ('./views/finance/blackTotalDetail.vue'),
            hidden: true,
        }],
        hidden: true
    },
    {
        path: '/warehose',
        component: Layout,
        name: '用户详情',
        children: [{
            path: '/warehose/warehoseDetails',
            component: () =>
                import ('./views/finance/WarehouseDetail.vue'),
            hidden: true,
        }],
        hidden: true
    },
    {
        path: '/purchase',
        component: Layout,
        name: '用户详情',
        children: [{
            path: '/purchase/purchaseDetails',
            component: () =>
                import ('./views/finance/purchaseDetails.vue'),
            hidden: true,
        }],
        hidden: true
    },

    {
        path: '/order',
        component: Layout,
        name: '订单详情',
        children: [{
            path: '/order/orderDetails',
            component: () =>
                import ('./views/order/orderDetails.vue'),
            hidden: true,
        }],
        hidden: true
    },
    {
        path: '/personal',
        component: Layout,
        name: '个人中心',
        children: [{
            path: '/personal/list',
            component: () =>
                import ('./views/personal/list.vue'),
            hidden: true,
        }],
        hidden: true
    },
    {
        path: '/',
        component: Layout,
        hidden: true
    },
    // {
    //   path: '/activityOrder',
    //   component: () =>
    //     import('./views/order/goodsOrder.vue'),
    //   hidden: true,
    // }
    // {
    //     path: '/',
    //     component: () =>
    //         import ('@/views/layout'),
    //     name: '主页',
    //     iconCls: 'el-icon-message', //图标样式class
    //     leaf: true, //只有一个节点
    //     children: [
    //         { path: '/', component: Main, name: '主页' },
    //     ]
    // },
    // {
    //     path: '/nav1',
    //     component: Layout,
    //     name: '导航一',
    //     iconCls: 'el-icon-message', //图标样式class
    //     children: [
    //         { path: '/nav1/table', component: Table, name: 'Table' },
    //         { path: '/nav1/form', component: Form, name: 'Form' },
    //         { path: '/nav1/user', component: user, name: '列表' },
    //     ]
    // },
    // {
    //     path: '/sys',
    //     component: Layout,
    //     name: '系统管理',
    //     iconCls: 'fa fa-id-card-o',
    //     children: [
    //         { path: '/sys/menu', component: menu, name: '菜单管理' },
    //         { path: '/sys/button', component: button, name: '按钮管理' },
    //         { path: '/sys/interface', component: interf, name: '接口列表' },
    //         { path: '/sys/role', component: role, name: '角色管理' },
    //         { path: '/sys/rolep', component: Page5, name: '权限管理' },
    //         { path: '/sys/user', component: Page5, name: '用户管理' }
    //     ]
    // },
    //   {
    //   path: '/nav3',
    //   component: Layout,
    //   name: '导航伞三',
    //   iconCls: 'fa fa-address-card',
    //   leaf: true, //只有一个节点
    //   children: [
    //     { path: '/nav3/page6', component: Page6, name: '导航三' }
    //   ]
    // },
    // {
    //     path: '/charts',
    //     component: Layout,
    //     name: 'Charts',
    //     iconCls: 'fa fa-bar-chart',
    //     children: [
    //         { path: '/charts/echarts', component: echarts, name: 'echarts' }
    //     ]
    // }
];

// add route path
routes.forEach(route => {
    route.path = route.path || '/' + (route.name || '');
});

const router = new Router({ routes });
router.beforeEach((to, from, next) => {
    const title = to && to.name;
    if (title) {
        document.title = title;
    }
    NProgress.start();
    if (to.path == '/Login') {
        next()
        NProgress.done()
        return
    }
    //获取权限
    permission.init(to.path, router.options.routes).then(function(d) {
        if (d) {
            if (d.code == 404) {
                next({ path: '/404' })
            } else if (d.code == 110 && to.path != '/Login') {
                next({ path: '/Login' })
            } else if (d.code == 1 && d.data) {
                //动态菜单
                d.data.forEach(e => {
                    if (e.path) {
                        e.component = () =>
                            import (`@/views${e.path}`)
                    }
                    e.children.forEach(c => {
                        if (c.path) {
                            c.component = () =>
                                import (`@/views${c.path}`)
                        }
                    })
                });
                // 将生成数组树结构的菜单并拼接404路由
                const routes = d.data.concat(router.options.routes);
                router.options.routes = routes;
                router.addRoutes(d.data);
                // 动态新增可访问路由表
                next({ path: to.path }); // hack方法 确保addRoutes已完成
            } else {
                next()
            }
        } else {
            next()
        }
        NProgress.done()
    });
});

router.afterEach(() => {
    NProgress.done() // finish progress bar
});
/**
 * 重写路由的push方法
 */
const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
    // console.log(location)
    return routerPush.call(this, location).catch(error => error)
}
Vue.use(Router);
export {
    router
};
// routes.forEach(route => {
//   route.path = route.path || '/' + (route.name || '');
// });

// const router = new Router({ routes });
// router.beforeEach((to, from, next) => {
//   const title = to && to.name;
//   if (title) {
//     document.title = title;
//   }
//   NProgress.start();
//   next();
// });

// router.afterEach(() => {
//   NProgress.done() // finish progress bar
// });
// /**
//  * 重写路由的push方法
//  */
// const routerPush = Router.prototype.push
// Router.prototype.push = function push (location) {
//   console.log(location)
//   return routerPush.call(this, location).catch(error => error)
// }
// Vue.use(Router);
// export {
//   router
// };